from django.shortcuts import render
from django.shortcuts import render
from rest_framework.views import APIView
from.sers import *
from rest_framework.response import Response
from django.db.models import F
from django.core.paginator import Paginator
class LoginApiview(APIView):
    # 登录
    def post(self,request):
        # 获取数据
        username = request.data.get("username")
        password = request.data.get("password")
        # 条件查询
        user = User.objects.filter(username=username,password=password)
        # 判断
        if user:
            return Response({"msg":"登录成功","status":"200"})
        else:
            return Response({"msg":"登录失败","status":"400"})
    def get(self,request):
        # 获取id
        id = request.GET.get("id")
        # 条件查询id
        i = News.objects.filter(id=id).first()
        w={"id":i.id,"title":i.title,"content":i.content,"cate":i.cate.name,"stars":i.stars}
        return Response(w)
class ShowApiview(APIView):
    # 分页查询
    def get(self,request):
        num = request.GET.get("num",1)
        num = int(num)
        queryset = News.objects.all()
        paginator = Paginator(queryset,2)
        page_list = paginator.get_page(num)
        ser = NewsSer(page_list,many=True)
        num_list = [x for x in paginator.page_range]
        resp = {
            "data":ser.data,
            "num":num_list
        }
        # 返回
        return Response(resp)